<?php


namespace App\Models;


use EclpOrderAddOrderRequest;
use JdClient;

class Jd
{
    //京东订单推送
    public static function pushJdOrder(Order $order)
    {
        $order_id = $order['id'];
        $orderlist = OrderItem::whereOrderId($order_id)->get()->toArray();
        $path = storage_path().'/logs/';
        file_put_contents($path.'jd-elcp-'.date('Y-m-d').'.log',json_encode($order).PHP_EOL, FILE_APPEND);
        if (empty($order)) {
            uexception('订单不存在');
        }
        if($order->is_pay != 1){
          uexception('未付款订单');
        }
        $jd_appkey = Rule::getValueByWhere('jd_appkey');
        $jd_appsecret = Rule::getValueByWhere('jd_appsecret');
        $jd_access_token = Rule::getValueByWhere('jd_access_token');

        $c = new JdClient();
        $c->appKey = $jd_appkey;
        $c->appSecret = $jd_appsecret;
        $c->accessToken = $jd_access_token;
        $c->serverUrl = "https://api.jd.com/routerjson";

        $req = new EclpOrderAddOrderRequest();
        //var_dump($req);die();
        $req->setIsvUUID( $order_id );//订单号
        $req->setIsvSource( "ISV0020000000068" );
        $req->setShopNo( "ESP0020000056883" ); // 店铺编号*
        $req->setDepartmentNo( "EBU4418046582179" );//事业部编号*
        $req->setWarehouseNo( "110008281" );//仓库编号
        $req->setShipperNo( "CYS0000010" );//承运商编号
        $req->setSalePlatformSource( "6" );//销售平台来源
        $req->setOrderMark( "00000000000000000000000000000000000000000000000000" );//订单标记位，首位为1代表货到付款 
        $req->setConsigneeName( $order->contact_name );//收货人姓名
        $req->setConsigneeMobile($order->contact_phone );//收货人手机
        $req->setConsigneePhone( $order->contact_phone ); //收货人电话
        $req->setConsigneeAddress($order->address_detail ); //收货地址
        $ids = '';
        $nums = '';
        $prices = '';
        $EMGcode = '';
        foreach( $orderlist as $v){
            $mall = ProductSku::find($v['product_sku_id']);
            $EMGcode .= !empty($EMGcode) ? ','.$mall['EMGcode'] : $mall['EMGcode'];
            $ids .= !empty($ids) ? ','.$v['product_id'] : $v['product_id'];
            $nums .= !empty($nums) ? ','.$v['amount']: $v['amount'];
            $prices .= !empty($prices) ? ','.$v['price'] : $v['price'];
        }
        file_put_contents($path.'/jd-param-'.date('Y-m-d').'.log',$EMGcode.'---'.$ids.'----'.$nums.'---'.$prices.PHP_EOL, FILE_APPEND);
        $req->setGoodsNo( $EMGcode );//京东EMG编码$mall->EMGcode
        $req->setIsvGoodsNo( $ids );//商家商品编号
        $req->setPrice( $prices );//商家金额
        $req->setQuantity( $nums ); //数量
        $req->setOrderPrice( $order->total_amount );
        $resp = $c->execute($req, $c->accessToken);
        file_put_contents($path.'/jd-uporder-'.date('Y-m-d').'.log',$order_id.'---'.json_encode($resp).PHP_EOL, FILE_APPEND);
        if($resp->code === "0"){
            order::updateJdNo($order_id, $resp->eclpSoNo);
            fsock_request(env('APP_URL').'/api/jd/notify',array('order_id'=>$order_id,'eclpSoNo'=>$resp->eclpSoNo));
        }else{
            //更新失败
            file_put_contents($path.'/jd-uperror-'.date('Y-m-d').'.log',$order_id.'---'.json_encode($resp).PHP_EOL, FILE_APPEND);
            uexception($resp);
        }
    }


}